<?php
include 'common/autoload.php';
include 'common/header.html';
include 'common/left.html';
include 'common/page.php';
include 'common/area.inc.php';
include 'common/Prod_Common.php';

//TODO:获取目前可销售的产品
$sql_chk_salelist = "SELECT fid,aid,pids FROM pft_product_sale_list WHERE fid={$_SESSION['memberID']} AND status=0";
$GLOBALS['le']->query($sql_chk_salelist);
$saling = array();
while($r=$GLOBALS['le']->fetch_assoc()) {
    $saling[$r['aid']] = $r['pids'];
}



$sql_get = "SELECT fid,pid,sid,sourceid,lvl FROM pft_p_apply_evolute WHERE fid={$_SESSION['memberID']}";
$GLOBALS['le']->query($sql_get);
$pid_arr = array();
$edatas = array();
while($r=$GLOBALS['le']->fetch_assoc()) {
    $edatas[] = $r;
    $pid_arr[] = $r['pid'];
    $sid_arr[] = $r['sid'];

}
if( count($pid_arr) ) {

    $pid_str = implode(',', $pid_arr);
    $sid_str = implode(',', $sid_arr);

    $sql_dname = "SELECT id,dname FROM pft_member WHERE id IN($sid_str)";
    $GLOBALS['le']->query($sql_dname);
    $dnames = array();
    while($dn = $GLOBALS['le']->fetch_assoc()) {
        $dnames[$dn['id']] = $dn['dname'];
    }

    $str = <<<SQL
        SELECT t.id as ticket_id,t.landid,t.title as ticket_title,t.tprice,t.px,t.pay,t.storage,
        t.s_limit_up,t.s_limit_low,t.delaydays,t.ddays,t.getaddr,
        t.buy_limit_up,t.buy_limit_low,t.apply_did,t.pid,l.imgpath,
        l.id,l.title,l.area,l.address,l.jtype,l.addtime,l.terminal,
        l.taobao_support,l.passtime,l.salerid,l.px,l.status,l.topic,
        l.keywords,l.description,l.apply_did,l.p_type,l.attribute
        FROM uu_jq_ticket t
        LEFT JOIN uu_land l ON t.landid=l.id
        WHERE t.pid IN($pid_str);
SQL;
    $GLOBALS['le']->query($str);
    $data = array();
    while($l=$GLOBALS['le']->fetch_assoc()) {
        $data[$l['pid']] = $l;
    }

    $sale_data = array();
    $today = date('Y-m-d');
    $soap = SoapInit::GetSoapInside();
    $sale = new \pft\Prod\Sale($GLOBALS['le'], $_SESSION['memberID']);
    $sort_pid = array();
    foreach($edatas as $ed) {
        $sort_pid[] = $ed['pid'];
        $fprice = $soap->get_Dynamic_Price_Merge($ed['pid'], $today, 1, '', '', 0);//当日供应价
        $uprice = $soap->get_Dynamic_Price_Merge($ed['pid'], $today, 1, '', '', 1);//当日零售价

        if($fprice==-1) {
            $fprice = $soap->get_Dynamic_Price_Merge($ed['pid'], '', 2, '', '', 0);//最低供应价
            $uprice = $soap->get_Dynamic_Price_Merge($ed['pid'], '', 2, '', '', 1);//最低零售价
        }
        $sid = $ed['lvl'] == 1 ? 0 : $ed['sid'];
        $diff_price = $sale->GetPriceSet($ed['pid'], $sid);

        $fprice = $fprice>-1 ? ($fprice/100 + $diff_price[$ed['pid']]/100) :'未配';
        $uprice = $uprice>-1 ? ($uprice/100 + $diff_price[$ed['pid']]/100) :'未配';

        $sale_data[] = array(
            'sid' => $ed['sid'],
            'lvl' => $ed['lvl'],
            'applyer_name' => $dnames[$ed['sid']],
            'fprice' => $fprice,
            'uprice'=> $uprice,
            'data' => $data[$ed['pid']]
        );
//

    }
    array_multisort($sort_pid,SORT_DESC, $sale_data);


//    $sale->GetPriceSet()
//    print_r($sale_data);
}
else {
//  echo "<script>alert('您目前没有可分销给其他供应商的产品！');</script>";
//  exit;
}
?>
    <link rel="stylesheet" type="text/css" href="css/aliceui.min.css"/>
    <link rel="stylesheet" type="text/css" href="css/c_common.css"/>
    <div class="clay">
        <div class="ui-tiptext-container ui-tiptext-container-success">
            <p class="ui-tiptext ui-tiptext-success">
                啦啦啦啦啦
            </p>
        </div>
        <iframe src="aboub:blank" style="height: 0;width: 0;display: none;" id="hideFrame" name="hideFrame" frameborder="0"></iframe>
        <form action="prod_sale.html" method="post" target="hideFrame">
            <input name="pid" value="<?=$_REQUEST['pid']?>" type="hidden"/>
            <div class="ui-table-container">
                <table class="ui-table">
                    <thead>
                    <tr>
                        <th>产品名称</th>
                        <th>供应商</th>
                        <th>供货价</th>
                        <th>零售价</th>
                        <th>配置</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?foreach($sale_data as $list):?>
                        <tr>
                            <td><?=$list['data']['title'].'-'.$list['data']['ticket_title']?></td>
                            <td><?=$list['applyer_name']?></td>
                            <td><?=$list['fprice']?></td>
                            <td><?=$list['uprice']?></td>
                            <td>
                                <input type="radio" name="prod_<?=$list['data']['pid']?>"
                                <?if( strpos($saling[$list['sid']],$list['data']['pid'])===false):?>
                                     checked="checked"/>
                                <a target="_blank" href="adjusting.html?pid=<?=$list['data']['pid']?>&sid=<?=$list['sid']?>&lvl=<?=$list['lvl']?>">配置</a>
                                <?else:?>
                                    />
                                <?endif;?>

                            </td>
                        </tr>
                    <?endforeach;?>
                    </tbody>
                </table>
            </div>
        </form>
    </div>
<?include('common/footer.html');?>